<?php

class Auth_Controller extends Base_Controller {

	public function action_index()
	{	
		return View::make('auth.index');
	}


    public function action_register()
    {   
        return View::make('auth.register');
    }

    public function action_addphoto()
    {

    }

    public function action_signup()
    {   
        $input = Input::all();

        $validation = User::validate($input);
        $validateProfile = UserProfile::validate($input);

        if( $validation->fails()) {
            return Redirect::to('auth/register')->with_errors($validation)->with_input();
        }elseif($validateProfile->fails()){
            return Redirect::to('auth/register')->with_errors($validateProfile)->with_input();
        }

       $resgisteredUser = User::registerUser($input);

       if($resgisteredUser){
            $mail = IoC::resolve('phpmailer');

            try {
                $mail->AddAddress( $input['emel'], $input['fullname'] );
                $mail->Subject  = "Setup 12345678";
                $mail->Body = 'To view the message, please use an HTML compatible email viewer!';
                // $mail->MsgHTML(file_get_contents('contents.html'));
                // $mail->AddAttachment('images/phpmailer.gif');      
                // $mail->AddAttachment('images/phpmailer_mini.gif'); 
                $mail->Send();
            } catch (Exception $e) {
                echo 'Message was not sent.';
                echo 'Mailer error: ' . $e->getMessage();
            }
       }

        return Redirect::to('auth');
    }

    public function action_authenticate()
    {
        $input = Input::all();
        
        $rules = array(
            'username' => 'required|exists:users',
            'password' => 'required'
        );
        
        $validation = Validator::make($input, $rules);
        
        if( $validation->fails() ) {
            return Redirect::to('auth')->with_errors($validation);
        }
        
        $credentials = array(
            'username' => $input['username'],
            'password' => $input['password']
        );

        if( Auth::attempt($credentials)) {
            return Redirect::to('dashboard');
        } else {
            Session::flash('login_errors', 'Your email or password is invalid - please try again.');
            return Redirect::to('auth');
        }
        // }
    }
    
    public function action_logout()
    {
        Auth::logout();
        return Redirect::to('home');
    }

    // public static function 


}

?>